package org.example.work2.at.order.service.impl;


import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.example.work2.at.order.dto.Order;
import org.example.work2.at.order.mapper.OrderMapper;
import org.example.work2.at.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public boolean createOrder(Order order) throws Exception {
        log.info("[createOrder] 当前 XID: {}", RootContext.getXID());

        // 保存订单
        log.info("[createOrder] 开始创建订单: {}", order);
        int saveOrder = orderMapper.saveOrder(order);
        if(saveOrder == 0){
            log.warn("[createOrder] 创建订单 {} 失败", order);
            throw new Exception("创建订单失败");
        }
        log.info("[createOrder] 创建订单成功: {}", order);
        return true;
    }

}
